﻿//#define DEBUG
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text.RegularExpressions;

namespace Icst.Leaflike.Web.controls
{
    public class Paginator : System.Web.UI.WebControls.WebControl
    {
        public enum CustomizeTextAlignEnum
        {
            Left = 0,
            Right = 1
        }

        private StringBuilder SbMainHtml = new StringBuilder();
        private const string CLEAR_PREFIX = "pg_";
        private int StartIndex = 0;
        private int EndIndex = 0;
        private int MaxNumBtnCount = 0;
        private bool _AlwayShow = true;

        public bool AlwayShow
        {
            get { return _AlwayShow; }
            set { _AlwayShow = value; }
        }

        private bool _AlwayShowPreviousNextFirstLast = false;
        public bool AlwayShowPreviousNextFirstLast
        {
            get { return _AlwayShowPreviousNextFirstLast; }
            set { _AlwayShowPreviousNextFirstLast = value; }
        }

        private string _GotoBoxTextBoxCssClass = CLEAR_PREFIX + "txt";
        public string GotoBoxTextBoxCssClass
        {
            get { return _GotoBoxTextBoxCssClass; }
            set { _GotoBoxTextBoxCssClass = value; }
        }

        private string _GotoBoxButtonCssClass = CLEAR_PREFIX + "btn";
        public string GotoBoxButtonCssClass
        {
            get { return _GotoBoxButtonCssClass; }
            set { _GotoBoxButtonCssClass = value; }
        }

        private string _PageListCssClass = null;
        public string PageListCssClass
        {
            get { return _PageListCssClass; }
            set { _PageListCssClass = value; }
        }

        private string _TagName = "div";
        public new string TagName
        {
            get { return _TagName; }
            set { _TagName = value; }
        }

        private string _PageUrlParameter = "page";
        public string PageUrlParameter
        {
            get { return _PageUrlParameter; }
            set { _PageUrlParameter = value; }
        }

        private int _PageSize = 20;
        public int PageSize
        {
            get { return _PageSize; }
            set
            {
                if (value < 1)
                {
                    throw new ArgumentException("页大小必须为正整数。");
                }

                _PageSize = value;
            }
        }

        private int? _PageIndex = null;
        public int PageIndex
        {
            get
            {
                if (_PageIndex == null)
                {
                    string tmpS = HttpContext.Current.Request.QueryString[PageUrlParameter];

                    if (string.IsNullOrEmpty(tmpS))
                    {
                        _PageIndex = 1;
                    }
                    else
                    {
                        int tmpI;

                        if (int.TryParse(tmpS, out tmpI))
                        {
                            _PageIndex = tmpI;
                        }
                        else
                        {
                            _PageIndex = 1;
                        }
                    }

                    //如果请求的页码大于最大页码，则返回当前最大页码
                    if (PageIndex > PageCount)
                    {
                        PageIndex = PageCount;
                    }
                }

                return _PageIndex.Value;
            }
            set
            {
                if (value < 1)
                {
                    throw new ArgumentException("参数 PageIndex（当前页码）必须为正整数。");
                }

                _PageIndex = value;
            }
        }

        private int _RecordCount = 0;
        public int RecordCount
        {
            get { return _RecordCount; }
            set
            {
                if (value < 0)
                {
                    throw new ArgumentException("参数 RecordCount（记录总数）必须为非负整数。");
                }

                _RecordCount = value;
            }
        }

        private int _PageCount = -1;
        public int PageCount
        {
            get
            {
                if (_PageCount < 0)
                {
                    _PageCount = Convert.ToInt32(Math.Ceiling(Convert.ToDouble(RecordCount) / Convert.ToDouble(PageSize)));
                }

                return _PageCount;
            }
        }

        private string _PreviousPageText = "上一页";
        public string PreviousPageText
        {
            get { return _PreviousPageText; }
            set { _PreviousPageText = value; }
        }

        private bool _ShowPreviousPage = true;
        public bool ShowPreviousPage
        {
            get { return _ShowPreviousPage; }
            set { _ShowPreviousPage = value; }
        }

        private string _NextPageText = "下一页";
        public string NextPageText
        {
            get { return _NextPageText; }
            set { _NextPageText = value; }
        }

        private bool _ShowNextPage = true;
        public bool ShowNextPage
        {
            get { return _ShowNextPage; }
            set { _ShowNextPage = value; }
        }

        private string _FirstPageText = "首页";
        public string FirstPageText
        {
            get { return _FirstPageText; }
            set { _FirstPageText = value; }
        }

        private string _GotoButtonText = "Go";
        public string GotoButtonText
        {
            get { return _GotoButtonText; }
            set { _GotoButtonText = value; }
        }

        private bool _ShowFirstPage = true;
        public bool ShowFirstPage
        {
            get { return _ShowFirstPage; }
            set { _ShowFirstPage = value; }
        }

        private string _LastPageText = "尾页";
        public string LastPageText
        {
            get { return _LastPageText; }
            set { _LastPageText = value; }
        }

        private bool _ShowLastPage = true;
        public bool ShowLastPage
        {
            get { return _ShowLastPage; }
            set { _ShowLastPage = value; }
        }

        private string _FirstPageCssClass = CLEAR_PREFIX + "fp";
        public string FirstPageCssClass
        {
            get { return _FirstPageCssClass; }
            set { _FirstPageCssClass = value; }
        }

        private string _PreviousPageCssClass = CLEAR_PREFIX + "pp";
        public string PreviousPageCssClass
        {
            get { return _PreviousPageCssClass; }
            set { _PreviousPageCssClass = value; }
        }

        private string _NextPageCssClass = CLEAR_PREFIX + "np";
         public string NextPageCssClass
        {
            get { return _NextPageCssClass; }
            set { _NextPageCssClass = value; }
        }

        private string _LastPageCssClass = CLEAR_PREFIX + "lp";
        public string LastPageCssClass
        {
            get { return _LastPageCssClass; }
            set { _LastPageCssClass = value; }
        }

        private int _NumericButtonCount = 10;
        public int NumericButtonCount
        {
            get { return _NumericButtonCount; }
            set { _NumericButtonCount = value; }
        }

        private string _NumericButtonTextFormatString = "<a href=\"{0}\">[{1}]</a>";
        public string NumericButtonTextFormatString
        {
            get { return _NumericButtonTextFormatString; }
            set { _NumericButtonTextFormatString = value; }
        }

        private string _CurrentNumericButtonCssClass = CLEAR_PREFIX + "cur";
        public string CurrentNumericButtonCssClass
        {
            get { return _CurrentNumericButtonCssClass; }
            set { _CurrentNumericButtonCssClass = value; }
        }

        private bool _ShowNumericButton = true;
        public bool ShowNumericButton
        {
            get { return _ShowNumericButton; }
            set { _ShowNumericButton = value; }
        }

        private string _MorePreviousNumericButtonText = "&lt;...";
        public string MorePreviousNumericButtonText
        {
            get { return _MorePreviousNumericButtonText; }
            set { _MorePreviousNumericButtonText = value; }
        }

        private string _MoreNextNumericButtonText = "...&gt;";
        public string MoreNextNumericButtonText
        {
            get { return _MoreNextNumericButtonText; }
            set { _MoreNextNumericButtonText = value; }
        }

        private bool _ShowMoreNumericButton = true;
        public bool ShowMoreNumericButton
        {
            get { return _ShowMoreNumericButton; }
            set { _ShowMoreNumericButton = value; }
        }

        private bool _ShowGotoBox = false;
        public bool ShowGotoBox
        {
            get { return _ShowGotoBox; }
            set { _ShowGotoBox = value; }
        }

        private bool _ShowPageIndexList = false;
        public bool ShowPageIndexList
        {
            get { return _ShowPageIndexList; }
            set { _ShowPageIndexList = value; }
        }

        private string _PagerTagName = null;
        public string PagerTagName
        {
            get { return _PagerTagName; }
            set { _PagerTagName = value; }
        }

        private string _PagerCssClass = CLEAR_PREFIX + "main";
        public string PagerCssClass
        {
            get { return _PagerCssClass; }
            set { _PagerCssClass = value; }
        }

        private string _CustomizeText = string.Empty;
        public string CustomizeText
        {
            get { return _CustomizeText; }
            set { _CustomizeText = value; }
        }

        private bool _ShowCustomizeText = false;
        public bool ShowCustomizeText
        {
            get { return _ShowCustomizeText; }
            set { _ShowCustomizeText = value; }
        }

        private CustomizeTextAlignEnum _CustomizeTextAlign = CustomizeTextAlignEnum.Left;
        public CustomizeTextAlignEnum CustomizeTextAlign
        {
            get { return _CustomizeTextAlign; }
            set { _CustomizeTextAlign = value; }
        }

        private string _UrlRewritePattern = string.Empty;
        public string UrlRewritePattern
        {
            get { return _UrlRewritePattern; }
            set { _UrlRewritePattern = value; }
        }

        private bool _EnableUrlRewrite = false;
        public bool EnableUrlRewrite
        {
            get { return _EnableUrlRewrite; }
            set { _EnableUrlRewrite = value; }
        }

        private string _UrlPathFilename = null;
        public string UrlPathFilename
        {
            get
            {
                if (_UrlPathFilename == null)
                {
                    string absolutePath = HttpContext.Current.Request.Url.AbsolutePath;

                    int pos = absolutePath.LastIndexOf('/');

                    if (pos == -1)
                    {
                        _UrlPathFilename = absolutePath;
                    }
                    else
                    {
                        _UrlPathFilename = absolutePath.Substring(pos + 1);
                    }

                    return _UrlPathFilename.Replace(".aspx", ".htm");
                }

                return _UrlPathFilename.Replace(".aspx",".htm");
            }
            set { _UrlPathFilename = value; }
        }

        private string _QueryFormatString = null;
        private string QueryFormatString
        {
            get
            {
                if (_QueryFormatString != null)
                {
                    return _QueryFormatString;
                }

                //1 如果启用了URL重写，则返回URL重写规则
                if (EnableUrlRewrite)
                {
                    _QueryFormatString = UrlRewritePattern;
                    return _QueryFormatString;
                }

                //2 否则从URL参数里解析并重新整理
                string tmpS = HttpContext.Current.Request.Url.Query;

                //处理为页码格式化字符串
                //1 如果URL有page参数
                if (HttpContext.Current.Request.QueryString[PageUrlParameter] != null)
                {
                    tmpS = Regex.Replace(tmpS, PageUrlParameter + "=\\d*", PageUrlParameter + "={0}");
                }
                //2 如果URL没有page参数
                else
                {
                    //1 如果URL有其它参数
                    if (tmpS.IndexOf('?') != -1)
                    {
                        tmpS += "&";
                    }

                    tmpS += PageUrlParameter + "={0}";
                }

                //删除第一个问号
                tmpS = Regex.Replace(tmpS, @"^\?", string.Empty);

                _QueryFormatString = string.Format("{0}?{1}", UrlPathFilename, tmpS);

                return _QueryFormatString;
            }
            set { _PageUrlParameter = value; }
        }

        private string _CustomizeFormatString = null;
        /// <summary>
        /// 自定义布局格式化字符串。
        /// {$text$} 自定义文本，
        /// {$first$} 首页，
        /// {$pre$} 上一页，
        /// {$num$} 数字页码，
        /// {$next$} 下一页，
        /// {$last$} 尾页，
        /// {$pre_more$} 前面还有更多页码，
        /// {$next_more$} 后面还有更多页码，
        /// {$list$} 页码下拉列表，
        /// {$goto$} 直接跳转到第N页快捷工具
        /// </summary>
        public string CustomizeLayoutFormatString
        {
            get { return _CustomizeFormatString; }
            set { _CustomizeFormatString = value; }
        }

        private bool _EnableCustomizeLayout = false;
        /// <summary>
        /// 是否启用自定义布局
        /// </summary>
        public bool EnableCustomizeLayout
        {
            get { return _EnableCustomizeLayout; }
            set { _EnableCustomizeLayout = value; }
        }

        private string _GotoAlertFormatString = "页码必须是数字且不能大于最大页码 {$pc$}。";
        /// <summary>
        /// 获取或设置[页码跳转]时输入的页码错误提示语，包含格式：{$pc$} 最大页码数
        /// </summary>
        public string GotoAlertFormatString
        {
            get { return _GotoAlertFormatString; }
            set { _GotoAlertFormatString = value; }
        }

        private string GetPageLink(int targetIndex)
        {
            return string.Format(QueryFormatString, targetIndex);
        }

        private string GetRelativePageLink(int relativePage)
        {
            return GetPageLink(PageIndex + relativePage);
        }

        private int GetRelativePageIndex(int relativePage)
        {
            return (PageIndex + relativePage);
        }

        private string GetGotoHtml()
        {
            string varName = CLEAR_PREFIX + "t_i";
            string txtVarName = CLEAR_PREFIX + "txt";
            string tmpS = "<input{$text_class$} type=\"text\" onmouseover=\"this.focus();this.select();\" onkeyup='if(event.keyCode==13){this.nextSibling.click();}' />"
                           + "<input{$btn_class$} type=\"button\" value=\"{$goto_btn_text$}\" onclick=\"var {$txt_var_name$}=this.previousSibling;var {$var_name$}=parseInt({$txt_var_name$}.value);"
                            + "if(!isNaN({$var_name$})&&Math.abs({$var_name$})<={$page_count$}&&Math.abs({$var_name$})!=0){"
                            + "location.href='{$url$}'.replace('{0}',Math.abs({$var_name$}));}"
                            + "else{alert('{$alert_string$}');{$txt_var_name$}.focus();{$txt_var_name$}.select();return;}\" />";
            tmpS = tmpS.Replace("{$var_name$}", varName)
                .Replace("{$page_count$}", PageCount.ToString())
                .Replace("{$url$}", QueryFormatString)
                .Replace("{$txt_var_name$}", txtVarName)
                .Replace("{$goto_btn_text$}", GotoButtonText)
                .Replace("{$text_class$}", string.IsNullOrEmpty(GotoBoxTextBoxCssClass) ? string.Empty : string.Format(" class=\"{0}\"", GotoBoxTextBoxCssClass))
                .Replace("{$btn_class$}", string.IsNullOrEmpty(GotoBoxButtonCssClass) ? string.Empty : string.Format(" class=\"{0}\"", GotoBoxButtonCssClass))
                .Replace("{$alert_string$}", GotoAlertFormatString.Replace("{$pc$}", PageCount.ToString()));


            return tmpS;
        }

        private string GetPageIndexList()
        {
            StringBuilder sbRtn = new StringBuilder();
            if (string.IsNullOrEmpty(PageListCssClass))
            {
                sbRtn.AppendFormat("<select onchange=\"location.href='{0}'.replace('{{0}}',this.value);\">", QueryFormatString);
            }
            else
            {
                sbRtn.AppendFormat("<select onchange=\"location.href='{0}'.replace('{{0}}',this.value);\" class=\"{1}\">", QueryFormatString, PageListCssClass);
            }

            for (int i = 1; i <= PageCount; i++)
            {
                if (i == PageIndex)
                {
                    sbRtn.AppendFormat("<option value=\"{0}\" selected=\"selected\">{0}</option>", i);
                }
                else
                {
                    sbRtn.AppendFormat("<option value=\"{0}\">{0}</option>", i);
                }
            }

            sbRtn.Append("</select>");
            return sbRtn.ToString();
        }


        private string GetMorePreviousNumericButton()
        {
            return string.Format(NumericButtonTextFormatString,
                     string.Format(QueryFormatString, StartIndex - 1), MorePreviousNumericButtonText);
        }

        private string GetMoreNextNumericButton()
        {
            return string.Format(NumericButtonTextFormatString,
                    string.Format(QueryFormatString, EndIndex + 1), MoreNextNumericButtonText);
        }

        private string GetNumericButtonHtml()
        {
            /******************************
             * MNC = maxNumBtnCount
             * PC = PageCount
             * P = PageIndex
             * MPP = midPagePos
             * LNC = leftNumCount
             * RNC = rightNumCount
             ******************************/
            StringBuilder sbRtn = new StringBuilder();
            if (PageCount < 2)
                return sbRtn.ToString();

            //如果开始页码不是1，则前面还页码
            if (StartIndex != 1 && ShowMoreNumericButton && !EnableCustomizeLayout)
            {
                sbRtn.AppendFormat(GetMorePreviousNumericButton());
            }

            for (int i = StartIndex; i <= EndIndex; i++)
            {
                string tmpS;

                if (i == PageIndex)
                {
                    tmpS = string.Format(NumericButtonTextFormatString,
                        ("javascript:void(0)") + string.Format("\" class=\"{0}", CurrentNumericButtonCssClass),
                        i);
                }
                else
                {
                    tmpS = string.Format(NumericButtonTextFormatString, string.Format(QueryFormatString, i), i);
                }

                sbRtn.Append(tmpS);
            } // end for

            //如果结束页码不是最大页码，则后面还有页码
            if (EndIndex != PageCount && ShowMoreNumericButton && !EnableCustomizeLayout)
            {
                sbRtn.AppendFormat(GetMoreNextNumericButton());
            }

            return sbRtn.ToString();
        }

        private string GetFirstPageHtml()
        {
            string valRtn;

            if (PageIndex <= 1)
            {
                valRtn = string.Format("<a{0} href=\"javascript:void(0)\">{1}</a>",
                    FirstPageCssClass.Length > 0 ? string.Format(" class=\"{0}\"", FirstPageCssClass) : string.Empty,
                    FirstPageText);
            }
            else
            {
                valRtn = string.Format("<a{0} href=\"{1}\">{2}</a>",
                    FirstPageCssClass.Length > 0 ? string.Format(" class=\"{0}\"", FirstPageCssClass) : string.Empty,
                    GetPageLink(1), FirstPageText);
            }

            return valRtn;
        }

        private string GetPreviousPageHtml()
        {
            string valRtn;
            int targetIndex = GetRelativePageIndex(-1);

            if (targetIndex > 0)
            {
                valRtn = string.Format("<a{0} href=\"{1}\">{2}</a>",
                    PreviousPageCssClass.Length > 0 ? string.Format(" class=\"{0}\"", PreviousPageCssClass) : string.Empty,
                    GetPageLink(targetIndex), PreviousPageText);
            }
            else
            {
                valRtn = string.Format("<a{0} href=\"javascript:void(0)\">{1}</a>",
                    PreviousPageCssClass.Length > 0 ? string.Format(" class=\"{0}\"", PreviousPageCssClass) : string.Empty,
                    PreviousPageText);
            }

            return valRtn;
        }

        private string GetNextPageHtml()
        {
            string valRtn;

            int targetIndex = GetRelativePageIndex(1);

            if (targetIndex <= PageCount)
            {
                valRtn = string.Format("<a{0} href=\"{1}\">{2}</a>",
                    NextPageCssClass.Length > 0 ? string.Format(" class=\"{0}\"", NextPageCssClass) : string.Empty,
                    GetPageLink(targetIndex), NextPageText);
            }
            else
            {
                valRtn = string.Format("<a{0} href=\"javascript:void(0)\">{1}</a>",
                    NextPageCssClass.Length > 0 ? string.Format(" class=\"{0}\"", NextPageCssClass) : string.Empty,
                    NextPageText);
            }

            return valRtn;
        }

        private string GetLastPageHtml()
        {
            string valRtn;

            if (PageIndex >= PageCount)
            {
                valRtn = string.Format("<a{0} href=\"javascript:void(0)\">{1}</a>",
                  LastPageCssClass.Length > 0 ? string.Format(" class=\"{0}\"", LastPageCssClass) : string.Empty,
                    LastPageText);
            }
            else
            {
                valRtn = string.Format("<a{0} href=\"{1}\">{2}</a>",
                    LastPageCssClass.Length > 0 ? string.Format(" class=\"{0}\"", LastPageCssClass) : string.Empty,
                    GetPageLink(PageCount), LastPageText);
            }

            return valRtn;
        }

        private void InitObjects()
        {
            MaxNumBtnCount = Math.Min(NumericButtonCount, PageCount);

            //1 如果MNC = PC
            if (MaxNumBtnCount == PageCount)
            {
                StartIndex = 1;
                EndIndex = PageCount;
            }
            //2 MNC > PC
            else
            {
                //第中间个页码位置
                int midPagePos;
                //左边页码个数
                int leftNumCount;
                //右边页码个数
                int rightNumCount;

                // 11/2 = 5
                midPagePos = (MaxNumBtnCount / 2);

                //tmpI = ;
                //如果MNC为偶数
                if ((MaxNumBtnCount % 2) == 0)
                {
                    leftNumCount = midPagePos - 1;
                    rightNumCount = midPagePos;
                }
                //如果MNC为奇数
                else
                {
                    leftNumCount = midPagePos;
                    rightNumCount = midPagePos;
                }

                StartIndex = (PageIndex - leftNumCount);
                EndIndex = (PageIndex + rightNumCount);


                while (StartIndex < 1 || EndIndex > PageCount)
                {
                    if (StartIndex < 1)
                    {
                        rightNumCount += (Math.Abs(StartIndex) + 1);
                        leftNumCount -= (Math.Abs(StartIndex) + 1);
                    }
                    else if (EndIndex > PageCount)
                    {
                        leftNumCount += (EndIndex - PageCount);
                        rightNumCount -= (EndIndex - PageCount);
                    }

                    StartIndex = (PageIndex - leftNumCount);
                    EndIndex = (PageIndex + rightNumCount);
                } // end while
            } // end if
        }

        private void ProcessPagerMain(HtmlTextWriter writer)
        {
            //容器开始
            if (!string.IsNullOrEmpty(TagName))
            {
                writer.WriteBeginTag(TagName);
                writer.Write(string.Format(" id=\"{0}\"", this.ClientID));

                if (!string.IsNullOrEmpty(CssClass))
                {
                    writer.Write(" class=\"" + CssClass + "\"");
                }

                writer.Write(">");
            }

            //自定义文本
            if (ShowCustomizeText && CustomizeTextAlign == CustomizeTextAlignEnum.Left)
            {
                writer.Write(CustomizeText);
            }

            //处理分页控件主要部分
            //如果有设置分页控件主要部分的窗口标签名称，则添加容器
            if (!string.IsNullOrEmpty(PagerTagName))
            {
                SbMainHtml.Append("<")
                          .Append(PagerTagName);

                if (!string.IsNullOrEmpty(PagerCssClass))
                {
                    SbMainHtml.AppendFormat(" class=\"{0}\"", PagerCssClass);
                }

                SbMainHtml.Append(">");
            }

            //添加[首页]按钮
            if (ShowFirstPage && (AlwayShowPreviousNextFirstLast || (PageIndex != 1 && PageCount > 0)))
            {
                SbMainHtml.Append(GetFirstPageHtml());
            }

            //添加[上一页]按钮
            if (ShowPreviousPage && (AlwayShowPreviousNextFirstLast || (PageIndex != 1 && PageCount > 0)))
            {
                SbMainHtml.Append(GetPreviousPageHtml());
            }

            if (ShowNumericButton)
            {
                SbMainHtml.Append(GetNumericButtonHtml());
            }

            //添加[下一页]按钮
            if (ShowNextPage && (AlwayShowPreviousNextFirstLast || (PageIndex != PageCount && PageCount > 0)))
            {
                SbMainHtml.Append(GetNextPageHtml());
            }

            //添加[尾页]按钮
            if (ShowLastPage && (AlwayShowPreviousNextFirstLast || (PageIndex != PageCount && PageCount > 0)))
            {
                SbMainHtml.Append(GetLastPageHtml());
            }

            //添加页码列表
            if (ShowPageIndexList)
            {
                SbMainHtml.Append(GetPageIndexList());
            }

            //添加[跳转到第N页]快捷工具
            if (ShowGotoBox)
            {
                SbMainHtml.Append(GetGotoHtml());
            }

            //如果有设置分页控件主要部分的窗口标签名称，则添加容器
            if (!string.IsNullOrEmpty(PagerTagName))
            {
                SbMainHtml.AppendFormat("</{0}>", PagerTagName);
            }

            writer.Write(SbMainHtml.ToString());

            //自定义文本
            if (ShowCustomizeText && CustomizeTextAlign == CustomizeTextAlignEnum.Right)
            {
                writer.Write(CustomizeText);
            }

            //容器结束
            if (!string.IsNullOrEmpty(TagName))
            {
                writer.WriteEndTag(TagName);
            }
        }

        private void ProcessCustomeLayout(HtmlTextWriter writer)
        {
            if (CustomizeLayoutFormatString == null)
            {
                throw new ArgumentNullException("启用自定义布局时，参数 CustomizeLayoutFormatString（自定义布局格式化字符串）不能为 null。");
            }

            //自定义布局格式字符串为空，无操作返回
            if (CustomizeLayoutFormatString.Length == 0)
            {
                return;
            }

            SbMainHtml = new StringBuilder(CustomizeLayoutFormatString);

            //自定义文本
            if (CustomizeLayoutFormatString.IndexOf("{$text$}") != -1)
            {
                SbMainHtml.Replace("{$text$}", CustomizeText);
            }

            //首页
            if (CustomizeLayoutFormatString.IndexOf("{$first$}") != -1)
            {
                SbMainHtml.Replace("{$first$}", GetFirstPageHtml());
            }

            //上一页
            if (CustomizeLayoutFormatString.IndexOf("{$pre$}") != -1)
            {
                SbMainHtml.Replace("{$pre$}", GetPreviousPageHtml());
            }

            //数字页码
            if (CustomizeLayoutFormatString.IndexOf("{$num$}") != -1)
            {
                SbMainHtml.Replace("{$num$}", GetNumericButtonHtml());
            }

            //下一页
            if (CustomizeLayoutFormatString.IndexOf("{$next$}") != -1)
            {
                SbMainHtml.Replace("{$next$}", GetNextPageHtml());
            }

            //尾页
            if (CustomizeLayoutFormatString.IndexOf("{$last$}") != -1)
            {
                SbMainHtml.Replace("{$last$}", GetLastPageHtml());
            }

            //前面还有更多页码
            if (CustomizeLayoutFormatString.IndexOf("{$pre_more$}") != -1)
            {
                SbMainHtml.Replace("{$pre_more$}", (StartIndex != 1 ? GetMorePreviousNumericButton() : string.Empty));
            }

            //后面还有更多页码
            if (CustomizeLayoutFormatString.IndexOf("{$next_more$}") != -1)
            {
                SbMainHtml.Replace("{$next_more$}", (EndIndex != PageCount ? GetMoreNextNumericButton() : string.Empty));
            }

            //页码下拉列表
            if (CustomizeLayoutFormatString.IndexOf("{$list$}") != -1)
            {
                SbMainHtml.Replace("{$list$}", GetPageIndexList());
            }

            //直接跳转到第N页快捷工具
            if (CustomizeLayoutFormatString.IndexOf("{$goto$}") != -1)
            {
                SbMainHtml.Replace("{$goto$}", GetGotoHtml());
            }

            writer.Write(SbMainHtml);
        }
        protected override void Render(HtmlTextWriter writer)
        {
            if ((PageCount > 0) || (PageCount < 1 && AlwayShow))
            {
                InitObjects();

                if (EnableCustomizeLayout)
                {
                    ProcessCustomeLayout(writer);
                }
                else
                {
                    ProcessPagerMain(writer);
                }
            }
        }
    }
}
